Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) E. Goods transportation 动态规划

E. Goods transportation

题目连接:

http://codeforces.com/contest/724/problem/E

Description

There are n cities located along the one-way road. Cities are numbered from 1 to n in the direction of the road.

The i-th city had produced pi units of goods. No more than si units of goods can be sold in the i-th city.

For each pair of cities i and j such that 1 ≤ i < j ≤ n you can no more than once transport no more than c units of goods from the city i to the city j. Note that goods can only be transported from a city with a lesser index to the city with a larger index. You can transport goods between cities in any order.

Determine the maximum number of produced goods that can be sold in total in all the cities after a sequence of transportations.

Input

The first line of the input contains two integers n and c (1 ≤ n ≤ 10 000, 0 ≤ c ≤ 109) — the number of cities and the maximum amount of goods for a single transportation.

The second line contains n integers pi (0 ≤ pi ≤ 109) — the number of units of goods that were produced in each city.

The third line of input contains n integers si (0 ≤ si ≤ 109) — the number of units of goods that can be sold in each city.

Output

Print the maximum total number of produced goods that can be sold in all cities after a sequence of transportations.

Sample Input

3 0
1 2 3
3 2 1

Sample Output

4

Hint

题意

给你n个城市,每个城市都可以往编号比自己大的城市运送c容量为物品

每个城市可以生产最多p[i]物品,最多售卖s[i]物品

然后问你这n个物品,最多卖多少物品,一共。

题解:

如果数据范围小的话,那么显然是网络流,直接莽一波就好了

但是这个过不了。

考虑最大流等于最小割,我们可以考虑dp[i][j]表示考虑i个点,我们割掉了j个汇点的最小花费。

那么这个dp[i][j]=min(dp[i-1][j-1]+s[i],dp[i-1][j]+p[i]+cj)

然后滚动数组优化一下就好了

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
int n;
long long c,p[maxn],s[maxn],f[maxn],ans,sum;
int main()
{
    cin>>n>>c;
    for(int i=1;i<=n;i++)cin>>p[i];
    for(int i=1;i<=n;i++)cin>>s[i];
    for(int i=1;i<=n;i++){
        f[i]=1e18;
        for(int j=i;j>=1;j--)
            f[j]=min(f[j]+j*c+p[i],f[j-1]+s[i]);
        f[0]+=p[i];
    }
    ans=1e18;
    for(int i=0;i<=n;i++)
        ans=min(ans,f[i]);
    cout<<ans<<endl;
}
posted @ 2016-10-10 17:58  qscqesze  阅读(340)  评论(0编辑  收藏  举报